package com.jh.user.business.impl;

import cn.jh.common.exception.ServiceException;
import com.jh.user.business.FastSilverPicPathBusiness;
import com.jh.user.pojo.FastSilverPicPath;
import com.jh.user.pojo.FastSilverPicPathRequest;
import com.jh.user.pojo.FastSilverPicPathResponse;
import com.jh.user.repository.FastSilverPicPathRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;

/**
 * @author zhugu
 * 2018/11/29 9:05
 */
@Service
public class FastSilverPicPathBusinessImpl implements FastSilverPicPathBusiness {
    private static final Logger log = LoggerFactory.getLogger(FastSilverPicPathBusinessImpl.class);

    @Value("${user.fastSilver.uploadPath}")
    private String uploadPath;

    @Value("${user.fastSilver.downloadPath}")
    private String downloadPath;

    private final FastSilverPicPathRepository fastSilverPicPathRepository;

    @Autowired
    FastSilverPicPathBusinessImpl(FastSilverPicPathRepository fastSilverPicPathRepository) {
        this.fastSilverPicPathRepository = fastSilverPicPathRepository;
    }

    @Override
    @Transactional
    public FastSilverPicPathResponse uploadPics(FastSilverPicPathRequest fsppRequest) throws ServiceException {


        FastSilverPicPathResponse fsppResponse = new FastSilverPicPathResponse();

        // 1、获取该用户上传的记录
        FastSilverPicPath exist = fastSilverPicPathRepository.queryByUserId(fsppRequest.getUserId());
        if (null == exist) {
            FastSilverPicPath fastSilverPicPath = new FastSilverPicPath();
            fastSilverPicPath.setUserId(fsppRequest.getUserId());
            fastSilverPicPath.setUpdateTime(Calendar.getInstance().getTime());
            fastSilverPicPath.setCreateTime(Calendar.getInstance().getTime());
            saveOrUpdateFastSilver(fsppRequest, fsppRequest.getUserId(), fsppResponse, fastSilverPicPath);
        } else {
            exist.setUpdateTime(Calendar.getInstance().getTime());
            saveOrUpdateFastSilver(fsppRequest, fsppRequest.getUserId(), fsppResponse, exist);
        }
        return fsppResponse;
    }

    /**
     * 保存或更新畅捷保存图片对象
     */
    private void saveOrUpdateFastSilver(FastSilverPicPathRequest fsppRequest, long userId, FastSilverPicPathResponse fsppResponse, FastSilverPicPath fastSilverPicPath) {
        if (fsppRequest.getMerchantHeadPic() != null && !fsppRequest.getMerchantHeadPic().isEmpty()) {
            fastSilverPicPath.setMerchantHead(uploadPic(userId, fsppRequest.getMerchantHeadPic(), "merchantHead"));
        }
        if (fsppRequest.getMerchantChechPic() != null && !fsppRequest.getMerchantChechPic().isEmpty()) {
            fastSilverPicPath.setMerchantChech(uploadPic(userId, fsppRequest.getMerchantChechPic(), "merchantChech"));
        }
        if (fsppRequest.getOtherPhotoThreePic() != null && !fsppRequest.getOtherPhotoThreePic().isEmpty()) {
            fastSilverPicPath.setOtherPhotoThree(uploadPic(userId, fsppRequest.getOtherPhotoThreePic(), "otherPhotoThree"));
        }
        if (fsppRequest.getIdentityFacePic() != null && !fsppRequest.getIdentityFacePic().isEmpty()) {
            fastSilverPicPath.setIdentityFace(uploadPic(userId, fsppRequest.getIdentityFacePic(), "identityFace"));
        }
        if (fsppRequest.getIdentityBackPic() != null && !fsppRequest.getIdentityBackPic().isEmpty()) {
            fastSilverPicPath.setIdentityBack(uploadPic(userId, fsppRequest.getIdentityBackPic(), "identityBack"));
        }
        if (fsppRequest.getBusinessCardPic() != null && !fsppRequest.getBusinessCardPic().isEmpty()) {
            fastSilverPicPath.setBusinessCard(uploadPic(userId, fsppRequest.getBusinessCardPic(), "businessCard"));
        }
        if (fsppRequest.getBusinessPic() != null && !fsppRequest.getBusinessPic().isEmpty()) {
            fastSilverPicPath.setBusiness(uploadPic(userId, fsppRequest.getBusinessPic(), "business"));
        }
        if (fsppRequest.getIdentityFaceCopyPic() != null && !fsppRequest.getIdentityFaceCopyPic().isEmpty()) {
            fastSilverPicPath.setIdentityFaceCopy(uploadPic(userId, fsppRequest.getIdentityFaceCopyPic(), "identityFaceCopy"));
        }
        if (fsppRequest.getIdentityBackCpoyPic() != null && !fsppRequest.getIdentityBackCpoyPic().isEmpty()) {
            fastSilverPicPath.setIdentityBackCpoy(uploadPic(userId, fsppRequest.getIdentityBackCpoyPic(), "identityBackCpoy"));
        }
        if (fsppRequest.getIdentityBodyPic() != null && !fsppRequest.getIdentityBodyPic().isEmpty()) {
            fastSilverPicPath.setIdentityBody(uploadPic(userId, fsppRequest.getIdentityBodyPic(), "identityBody"));
        }
        if (fsppRequest.getOtherPhotoFourPic() != null && !fsppRequest.getOtherPhotoFourPic().isEmpty()) {
            fastSilverPicPath.setOtherPhotoFour(uploadPic(userId, fsppRequest.getOtherPhotoFourPic(), "otherPhotoFour"));
        }
        if (fsppRequest.getOtherPhotoTwoPic() != null && !fsppRequest.getOtherPhotoTwoPic().isEmpty()) {
            fastSilverPicPath.setOtherPhotoTwo(uploadPic(userId, fsppRequest.getOtherPhotoTwoPic(), "otherPhotoTwo"));
        }
        if (fsppRequest.getOtherPhotoPic() != null && !fsppRequest.getOtherPhotoPic().isEmpty()) {
            fastSilverPicPath.setOtherPhoto(uploadPic(userId, fsppRequest.getOtherPhotoPic(), "otherPhoto"));
        }
        // 新增图片
        if (fsppRequest.getFirstPhotoPic() != null && !fsppRequest.getFirstPhotoPic().isEmpty()) {
            fastSilverPicPath.setFirstPhoto(uploadPic(userId, fsppRequest.getFirstPhotoPic(), "firstPhoto"));
        }
        if (fsppRequest.getBankFrontPhotoPic() != null && !fsppRequest.getBankFrontPhotoPic().isEmpty()) {
            fastSilverPicPath.setBankFrontPhoto(uploadPic(userId, fsppRequest.getBankFrontPhotoPic(), "bankFrontPhoto"));
        }
        if (fsppRequest.getBankReversePhotoPic() != null && !fsppRequest.getBankReversePhotoPic().isEmpty()) {
            fastSilverPicPath.setBankReversePhoto(uploadPic(userId, fsppRequest.getBankReversePhotoPic(), "bankReversePhoto"));
        }
        if (fsppRequest.getMchScenePhotoPic() != null && !fsppRequest.getMchScenePhotoPic().isEmpty()) {
//            fastSilverPicPath.setMchScenePhoto(uploadPic(userId, fsppRequest.getMchScenePhotoPic(), "mchScenePhoto"));
            fastSilverPicPath.setOtherPhotoThree(uploadPic(userId, fsppRequest.getMchScenePhotoPic(), "otherPhotoThree"));
        }
        if (fsppRequest.getRelationshipPhotoPic() != null && !fsppRequest.getRelationshipPhotoPic().isEmpty()) {
            fastSilverPicPath.setRelationshipPhoto(uploadPic(userId, fsppRequest.getRelationshipPhotoPic(), "relationshipPhoto"));
        }
        if (fsppRequest.getMerchantPhotoPic() != null && !fsppRequest.getMerchantPhotoPic().isEmpty()) {
            fastSilverPicPath.setMerchantPhoto(uploadPic(userId, fsppRequest.getMerchantPhotoPic(), "merchantPhoto"));
        }
        FastSilverPicPath save = fastSilverPicPathRepository.save(fastSilverPicPath);
        fsppResponse.addFastSilverPicPathResponse(save);
    }

    /**
     * 上传图片
     * @param userId 用户id
     * @param file 图片文件
     * @param picName
     * @return
     * @throws ServiceException
     */
    private String uploadPic(long userId, MultipartFile file, String picName) throws ServiceException{
        // 1、创建目录
        String uploadPathStr = uploadPath + userId + File.separator;
        File createFile = new File(uploadPathStr);
        boolean mkdirs = createFile.mkdirs();
        if (!mkdirs) {
            log.warn("创建{}目录失败, 请检查权限！", uploadPathStr);
        }

        String originalFilename = picName + "-" + userId + ".png";

        File merchantHeadFile = new File(uploadPathStr + originalFilename);

        // 上传图片
        try {
            file.transferTo(merchantHeadFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 设置图片下载地址
        return downloadPath + userId + File.separator + originalFilename;
    }

    @Override
    public FastSilverPicPathResponse findFsppList(long userId) throws ServiceException {
        FastSilverPicPath fastSilverPicPath = fastSilverPicPathRepository.queryByUserId(userId);
        if (null == fastSilverPicPath) {
            return new FastSilverPicPathResponse();
        }
        FastSilverPicPathResponse fsppResponse = new FastSilverPicPathResponse();
        fsppResponse.addFastSilverPicPathResponse(fastSilverPicPath);
        return fsppResponse;
    }
}
